package com.client.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.client.model.SdkIPayData;
import com.client.model.SdkIPayOrder;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ModelGenerator;

public class SdkPayDataServiceImpl extends AbstractService implements
SdkPayDataService {
	private Logger logger;
	
	public SdkPayDataServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	public List<SdkIPayOrder> getPayOrder(String date1,String date2,String mobile,String orderId)
	{
		String sql = "select id,orderid,suborderid,userid,mobileNo,carrier," +
				"province,region,feevalue," +
				"channelid,projectid,report,ordertime orderTime1,reporttime reporttime1" +
				",'0' errorNum,ordertime ordertime,reporttime reporttime," +
				"cpid,appid,goodscode,goodsname,chid,requestfee,successfee,sdkid,reportsdkid,isfilter " +
				" from sdkipayorder where ordertime between '"+date1+"' and '"+date2+"' ";
		if(mobile!=null && mobile.length()>0)
			sql+=" and mobileNo='"+mobile+"'";
		if(orderId!=null && orderId.length()>0)
			sql+=" and orderid='"+orderId+"'";
		sql+=" order by ordertime desc";
		ModelGenerator<SdkIPayOrder> dbcpmg = new ModelGenerator<SdkIPayOrder>(SdkIPayOrder.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	@Override
	public List<SdkIPayData> getPayDataByContent(String date1,String date2,
			int channel, int project, String province,
			String region,String dimension,Account operator,int cpid,String appnumber,String sdkid) {
		String sql = "";
		{
			String gb = "";
			List<String> fl = new ArrayList<String>();
			
			if (!checkQueryDimension(dimension, "day"))
				gb += "0 ";
			else
				fl.add("day");
			gb += "day,";
			
			if (!checkQueryDimension(dimension, "hour"))
				gb += "0 ";
			else
				fl.add("hour");
			gb += "hour,";
			
			if (!checkQueryDimension(dimension, "minute"))
				gb += "0 ";
			else
				fl.add("minute");
			gb += "minute,";
			
			
			
			if (!checkQueryDimension(dimension, "carrier"))
				gb += "0 ";
			else
				fl.add("carrier");
			gb += "carrier,";
			
			if (!checkQueryDimension(dimension, "channel"))
				gb += "-1 ";
			else
				fl.add("channel");
			gb += "channel,";

			if (!checkQueryDimension(dimension, "project"))
				gb += "-1 ";
			else
				fl.add("project");
			gb += "project,";

			if (!checkQueryDimension(dimension, "province"))
				gb += "'' ";
			else
				fl.add("province");
			gb += "province,";

			if (!checkQueryDimension(dimension, "region"))
				gb += "'' ";
			else
				fl.add("region");
			gb += "region,";
			
			
			
			if (!checkQueryDimension(dimension, "cpid"))
				gb += "'' ";
			else
				fl.add("cpid");
			gb += "cpid,";
			
			if (!checkQueryDimension(dimension, "appid"))
				gb += "'' ";
			else
				fl.add("appid");
			gb += "appid,";
			
			if (!checkQueryDimension(dimension, "goodscode"))
				gb += "'' goodscode,'' goodsname,";
			else
			{
				fl.add("goodscode");
				fl.add("goodsname");
				gb += "goodscode,goodsname,";
			}
			
			if (!checkQueryDimension(dimension, "chid"))
				gb += "'' ";
			else
				fl.add("chid");
			gb += "chid,";
			
			if (!checkQueryDimension(dimension, "sdkid"))
				gb += "'' ";
			else
				fl.add("sdkid");
			gb += "sdkid,";
			
			sql = "select 0 id," + gb + "count,issuedcount,resultcount,succ,feevalue,requestfee,successfee " +
				"from (select " + fl.toString().replace("[", "").replace("]", "").replace(" ", "") +
				",sum(count) count,sum(issuedcount) issuedcount,sum(resultcount) resultcount,sum(succ) succ,sum(feevalue) feevalue" +
				" ,sum(requestfee) requestfee,sum(successfee) successfee from sdkipaydata " +
				"where (day>=" + date1.replace("-", "")  + " " +
				"and day<=" + date2.replace("-", "") + ") ";
			if (channel > -1)
				sql += " and channel=" + channel + " ";
//			else
//				sql += " and channel in (select channelid from accountchannel where accountid='" + operator.getId() + "') ";
			if (project > -1)
				sql += " and project=" + project + " ";
			
			if (province != null && !province.equalsIgnoreCase("ALL"))
				sql += "and province='" + province +"' ";
			if (region != null && !region.equalsIgnoreCase("ALL"))
				sql += "and region='" + region + "' ";
			
			
			if (cpid > -1)
				sql += " and cpid=" + cpid + " ";
			
			if (appnumber != null && !appnumber.equalsIgnoreCase("ALL"))
				sql += "and appid='" + appnumber +"' ";
			
			if (sdkid != null && !sdkid.equalsIgnoreCase("ALL"))
				sql += "and sdkid='" + sdkid +"' ";
			
			sql += "group by " + fl.toString().replace("[", "").replace("]", "").replace(" ", "") + ") a " +
					"order by day,hour,minute,channel,project,province,region";
		}
		ModelGenerator<SdkIPayData> dbcpmg = new ModelGenerator<SdkIPayData>(SdkIPayData.class);
		return dbcpmg.generateList(dbcp, sql);
	}

}
